package com.niit.session23.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectDemo {
	public static void main(String[] args) {
		User user = new User("Trout");
		FileOutputStream fos;
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		try {
//			序列化：把对象写到文件中
			fos = new FileOutputStream("G:\\Java\\input\\outputObj.txt");
			oos = new ObjectOutputStream(fos);
			oos.writeObject(user);
//			反序列化：把文件中的对象读取到内存中，也就是程序中
			FileInputStream fis = new FileInputStream("G:\\Java\\input\\outputObj.txt");
			ois = new ObjectInputStream(fis);
			User user1 = (User)ois.readObject();
			System.out.println(user1);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally {
			try {
				oos.close();
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
